/*
 * Copyright 2025 arisgo@163.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.arisgo.cloud.common;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;

/**
 * 结果
 * @author Coulson
 * @since 2023-08-30
 */
public class Pagination<T> implements Serializable {

    public static final int DEFAULT_PAGE = 1;
    public static final int DEFAULT_PAGE_SIZE = 10;

    private List<T> records;
    private long pageNo;
    private long pageSize;
    /**
     * 总页数
     */
    private long pageCount;
    /**
     * 数据总数
     */
    private long total;

    private Pagination(long pageNo, long pageSize, long pageCount, long total, List<T> records) {
        this.pageNo = pageNo;
        this.pageSize = pageSize;
        this.pageCount = pageCount;
        this.total = total;
        this.records = records;
    }

    public List<T> getRecords() {
        return records;
    }

    public void setRecords(List<T> records) {
        this.records = records;
    }

    public long getPageNo() {
        return pageNo;
    }

    public void setPageNo(int pageNo) {
        this.pageNo = pageNo;
    }

    public long getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public long getPageCount() {
        return pageCount;
    }

    public void setPageCount(int pageCount) {
        this.pageCount = pageCount;
    }

    public long getTotal() {
        return total;
    }

    public void setTotal(long total) {
        this.total = total;
    }

    public static <T> Pagination<T> creator(long pageNo, long pageSize, long total, List<T> records) {
        long pageCount = total % pageSize == 0 ? total / pageSize : total / pageSize + 1;
        return new Pagination<>(pageNo, pageSize, pageCount, total, records);
    }

    public static <T> Pagination<T> creator(long pageNo, long pageSize, long pageCount, long total, List<T> records) {
        return new Pagination<>(pageNo, pageSize, pageCount, total, records);
    }

    public static <T> Pagination<T> defaultPage() {
        return new Pagination<>(DEFAULT_PAGE, DEFAULT_PAGE_SIZE, 0, 0, Collections.emptyList());
    }

    /**
     * 首页从 1 开始
     *
     * @param pageNo   当前页
     * @param pageSize 每页数量
     * @param data     数据
     */
    public static <T> Pagination<T> paging(long pageNo, long pageSize, List<T> data) {
        if (null == data || data.isEmpty()) {
            return Pagination.defaultPage();
        }
        if (pageNo < 1) {
            throw new RuntimeException("分页参数从 1 开始！");
        }
        long total = data.size();
        long pageCount = total % pageSize == 0 ? total / pageSize : total / pageSize + 1;
        if (pageNo > pageCount) {
            return Pagination.defaultPage();
        }
        Long offset = (pageNo - 1) * pageSize;
        if (offset.compareTo(total) > 0) {
            return Pagination.defaultPage();
        }

        Long end = offset + pageSize;
        if (end.compareTo(total) > 0) {
            end = total;
        }
        return Pagination.creator(pageNo, pageSize, pageCount, total, data.subList(offset.intValue(), end.intValue()));
    }

}
